home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_061 / microemacs / crypt.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  7KB  |  218 lines

  1. /*    Crypt:    Encryption routines for MicroEMACS
  2.         written by Dana Hoggatt and Daniel Lawrence
  3. */
  4.  
  5. #include    <stdio.h>
  6. #include    "estruct.h"
  7. #include    "edef.h"
  8.  
  9. #if    CRYPT
  10.  
  11. #if    MEGAMAX & ST520
  12. overlay    "crypt"
  13. #endif
  14.  
  15. setkey(f, n)    /* reset encryption key of current buffer */
  16.  
  17. int f;        /* default flag */
  18. int n;        /* numeric argument */
  19.  
  20. {
  21.     register int status;    /* return status */
  22.     char key[NPAT];        /* new encryption string */
  23.  
  24.     /* get the string to use as an encrytion string */
  25.         if ((status = mlreply("Encryption String: ", key, NPAT - 1)) != TRUE)
  26.                 return(status);
  27.  
  28.     /* and encrypt it */
  29.     crypt((char *)NULL, 0);
  30.     crypt(key, strlen(key));
  31.  
  32.     /* and save it off */
  33.     strcpy(curbp->b_key, key);
  34.     mlwrite(" ");        /* clear it off the bottom line */
  35.     return(TRUE);
  36. }
  37.  
  38. /**********
  39.  *
  40.  *    crypt - in place encryption/decryption of a buffer
  41.  *
  42.  *    (C) Copyright 1986, Dana L. Hoggatt
  43.  *    1216, Beck Lane, Lafayette, IN
  44.  *
  45.  *    When consulting directly with the author of this routine, 
  46.  *    please refer to this routine as the "DLH-POLY-86-B CIPHER".  
  47.  *
  48.  *    This routine was written for Dan Lawrence, for use in V3.8 of
  49.  *    MICRO-emacs, a public domain text/program editor.  
  50.  *
  51.  *    I kept the following goals in mind when preparing this function:
  52.  *
  53.  *        1.    All printable characters were to be encrypted back
  54.  *        into the printable range, control characters and
  55.  *        high-bit characters were to remain unaffected.  this
  56.  *        way, encrypted would still be just as cheap to 
  57.  *        transmit down a 7-bit data path as they were before.
  58.  *
  59.  *        2.    The encryption had to be portable.  The encrypted 
  60.  *        file from one computer should be able to be decrypted 
  61.  *        on another computer.
  62.  *
  63.  *        3.    The encryption had to be inexpensive, both in terms
  64.  *        of speed and space.
  65.  *
  66.  *        4.    The system needed to be secure against all but the
  67.  *        most determined of attackers.
  68.  *
  69.  *    For encryption of a block of data, one calls crypt passing 
  70.  *    a pointer to the data block and its length. The data block is 
  71.  *    encrypted in place, that is, the encrypted output overwrites 
  72.  *    the input.  Decryption is totally isomorphic, and is performed 
  73.  *    in the same manner by the same routine.  
  74.  *
  75.  *    Before using this routine for encrypting data, you are expected 
  76.  *    to specify an encryption key.  This key is an arbitrary string,
  77.  *    to be supplied by the user.  To set the key takes two calls to 
  78.  *    crypt().  First, you call 
  79.  *
  80.  *        crypt(NULL, vector)
  81.  *
  82.  *    This resets all internal control information.  Typically (and 
  83.  *    specifically in the case on MICRO-emacs) you would use a "vector" 
  84.  *    of 0.  Other values can be used to customize your editor to be 
  85.  *    "incompatable" with the normally distributed version.  For 
  86.  *    this purpose, the best results will be obtained by avoiding
  87.  *    multiples of 95.
  88.  *
  89.  *    Then, you "encrypt" your password by calling 
  90.  *
  91.  *        crypt(pass, strlen(pass))
  92.  *
  93.  *    where "pass" is your password string.  Crypt() will destroy 
  94.  *    the original copy of the password (it becomes encrypted), 
  95.  *    which is good.  You do not want someone on a multiuser system 
  96.  *    to peruse your memory space and bump into your password.  
  97.  *    Still, it is a better idea to erase the password buffer to 
  98.  *    defeat memory perusal by a more technical snooper.  
  99.  *
  100.  *    For the interest of cryptologists, at the heart of this 
  101.  *    function is a Beaufort Cipher.  The cipher alphabet is the 
  102.  *    range of printable characters (' ' to '~'), all "control" 
  103.  *    and "high-bit" characters are left unaltered.
  104.  *
  105.  *    The key is a variant autokey, derived from a wieghted sum 
  106.  *    of all the previous clear text and cipher text.  A counter 
  107.  *    is used as salt to obiterate any simple cyclic behavior 
  108.  *    from the clear text, and key feedback is used to assure 
  109.  *    that the entire message is based on the original key, 
  110.  *    preventing attacks on the last part of the message as if 
  111.  *    it were a pure autokey system.
  112.  *
  113.  *    Overall security of encrypted data depends upon three 
  114.  *    factors:  the fundamental cryptographic system must be 
  115.  *    difficult to compromise; exhaustive searching of the key 
  116.  *    space must be computationally expensive; keys and plaintext 
  117.  *    must remain out of sight.  This system satisfies this set
  118.  *    of conditions to within the degree desired for MicroEMACS.
  119.  *
  120.  *    Though direct methods of attack (against systems such as 
  121.  *    this) do exist, they are not well known and will consume 
  122.  *    considerable amounts of computing time.  An exhaustive
  123.  *    search requires over a billion investigations, on average.
  124.  *
  125.  *    The choice, entry, storage, manipulation, alteration, 
  126.  *    protection and security of the keys themselves are the 
  127.  *    responsiblity of the user.  
  128.  *
  129.  **********/
  130.  
  131. crypt(bptr, len)
  132. register char *bptr;    /* buffer of characters to be encrypted */
  133. register unsigned len;    /* number of characters in the buffer */
  134. {
  135.     register int cc;    /* current character being considered */
  136.  
  137.     static long key = 0;    /* 29 bit encipherment key */
  138.     static int salt = 0;    /* salt to spice up key with */
  139.  
  140.     if (!bptr) {        /* is there anything here to encrypt? */
  141.         key = len;    /* set the new key */
  142.         salt = len;    /* set the new salt */
  143.         return;
  144.     }
  145.     while (len--) {        /* for every character in the buffer */
  146.  
  147.         cc = *bptr;    /* get a character out of the buffer */
  148.  
  149.         /* only encipher printable characters */
  150.         if ((cc >= ' ') && (cc <= '~')) {
  151.  
  152. /**  If the upper bit (bit 29) is set, feed it back into the key.  This 
  153.     assures us that the starting key affects the entire message.  **/
  154.  
  155.             key &= 0x1FFFFFFFL;    /* strip off overflow */
  156.             if (key & 0x10000000L) {
  157.                 key ^= 0x0040A001L;    /* feedback */
  158.             }
  159.  
  160. /**  Down-bias the character, perform a Beaufort encipherment, and 
  161.     up-bias the character again.  We want key to be positive 
  162.     so that the left shift here will be more portable and the 
  163.     mod95() faster   **/
  164.  
  165.             cc = mod95((int)(key % 95) - (cc - ' ')) + ' ';
  166.  
  167. /**  the salt will spice up the key a little bit, helping to obscure 
  168.     any patterns in the clear text, particularly when all the 
  169.     characters (or long sequences of them) are the same.  We do 
  170.     not want the salt to go negative, or it will affect the key 
  171.     too radically.  It is always a good idea to chop off cyclics 
  172.     to prime values.  **/
  173.  
  174.             if (++salt >= 20857) {    /* prime modulus */
  175.                 salt = 0;
  176.             }
  177.  
  178. /**  our autokey (a special case of the running key) is being 
  179.     generated by a wieghted checksum of clear text, cipher 
  180.     text, and salt.   **/
  181.  
  182.             key = key + key + cc + *bptr + salt;
  183.         }
  184.         *bptr++ = cc;    /* put character back into buffer */
  185.     }
  186.     return;
  187. }
  188.  
  189. #if    MEGAMAX & ST520
  190. int mod95(val)
  191. #else
  192. static int mod95(val)
  193. #endif
  194.  
  195. register int val;
  196.  
  197. {
  198.     /*  The mathematical MOD does not match the computer MOD  */
  199.  
  200.     /*  Yes, what I do here may look strange, but it gets the
  201.         job done, and portably at that.  */
  202.  
  203.     while (val >= 9500)
  204.         val -= 9500;
  205.     while (val >= 950)
  206.         val -= 950;
  207.     while (val >= 95)
  208.         val -= 95;
  209.     while (val < 0)
  210.         val += 95;
  211.     return (val);
  212. }
  213. #else
  214. nocrypt()
  215. {
  216. }
  217. #endif
  218.